//
//  TDInputPasswordC.m
//  TBRentClient
//
//  Created by tonbright on 2018/6/20.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TDInputPasswordC.h"
#import "NetWorkRequest.h"
#import "NSString+MD5Addition.h"
@interface TDInputPasswordC ()

@property (weak, nonatomic) IBOutlet UIView *view_agreeLaw;

@property (weak, nonatomic) IBOutlet UIImageView *imv_isAgreeLawSel;
@property (weak, nonatomic) IBOutlet UILabel *lbe_agreeLaw;
@property (weak, nonatomic) IBOutlet UITextField *tf_password;

@property (nonatomic, assign) BOOL isAgree;
@end

@implementation TDInputPasswordC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.tf_password becomeFirstResponder];
}

- (IBAction)closeBtnClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    if (!_isRegister) {
        _view_agreeLaw.hidden = YES;
    }
    _isAgree = YES;
}


- (IBAction)showOrHidePWBtnClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        _tf_password.secureTextEntry = NO;
    }else {
        _tf_password.secureTextEntry = YES;
    }
}


// MARK: - 完成按钮点击----注册页面跳转到开始
- (IBAction)finishBtnClick:(UIButton *)sender {
    if (!_isAgree) {
        self.noticeText = @"请先同意完成按钮下面的相关条款";
        return;
    }
    [self finishClick];
}

- (IBAction)agreeOrShowLawBtnClick:(UIButton *)sender {
    //点击同意按钮
    if (sender.tag == 10) {
        if (_isAgree) {
            _imv_isAgreeLawSel.image = [UIImage imageNamed:@"试驾圆未选中"];
            _isAgree = NO;
        }else {
            _imv_isAgreeLawSel.image = [UIImage imageNamed:@"试驾圆选中"];
            _isAgree = YES;
        }
    }else {
        
    }
}


// MARK: - 获取验证码网络事件-------
- (void)finishClick {
    NSString *phone = [AppSetting shareAppSetting].phone;
    NSString *topcompanyid = [AppSetting shareAppSetting].topcompanyid;
    NSString *passwordStr = self.tf_password.text;
    NSString *verifycode = [AppSetting shareAppSetting].verifycode;
    NSString *devicetoken = [AppSetting shareAppSetting].devicetoken;
    
    //输入只能是字母和数字---------------判断账号输入的类型-------------
    NSCharacterSet *s = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_"];
    s = [s invertedSet];
    NSRange r = [passwordStr rangeOfCharacterFromSet:s];
    if (passwordStr.length == 0) {
        self.noticeText = @"密码不能为空";
        return;
    }
    else if (r.location != NSNotFound) {
        self.noticeText = @"不能输入字母数字_以外的字符";
        return;
    }  else if (passwordStr.length < 6) {
        self.noticeText = @"密码不能小于6位";
        return;
    } else if (passwordStr.length > 20) {
        self.noticeText = @"密码不能大于20位";
        return;
    }
    
    NSDictionary *parDic = @{@"phone":phone,@"password":[passwordStr MD5String],@"verifycode":verifycode,@"devicetoken":@"ios"};
    __weak typeof(self) weakSelf = self;
    
    NSString *url = RegisterTD;
    if (!_isRegister) {
        url = ResetPwdTD;
        parDic = @{@"phone":phone,@"newpwd":[passwordStr MD5String],@"verifycode":verifycode,@"devicetoken":@"ios"};
    }
    
    [self progressHUDShowWithText:nil];
    [NetWorkRequest postNetRequestData:url parmeters:parDic response:^(NSDictionary *response) {
        if (response) {
            //调用登录接口
            if (weakSelf.isRegister) {
                [weakSelf loginBtnDidClick:parDic];
                return;
            }
            [weakSelf progressHUDHideImmediately];
            [weakSelf.navigationController popToRootViewControllerAnimated:YES];
            
        }else {
            [weakSelf progressHUDHideImmediately];
        }
    } netError:^(NSError *error) {
        [weakSelf progressHUDShowWithCompleteText:nil isSucceed:NO];
    }];
}


- (void)loginBtnDidClick:(NSDictionary *)dic {
    [self.view endEditing:YES];
    __weak typeof(self) weakSelf = self;
    [self progressHUDShowWithText:@"登录中..."];
    // MARK: - 登录操作--------------
    [NetWorkRequest postNetRequestData:LoginTD parmeters:dic response:^(NSDictionary *response) {
        if (response) {
            //保存数据---进行一系列的操作----------
            [weakSelf progressHUDShowWithCompleteText:@"登录成功" isSucceed:YES];
            [AppSetting shareAppSetting].isLogin = @"1";
            
            [AppSetting shareAppSetting].memberid = response[@"data"][@"memberid"];
            [AppSetting shareAppSetting].tokenid = response[@"data"][@"tokenid"];
            
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        }else {
            [weakSelf progressHUDHideImmediately];
        }
    } netError:^(NSError *error) {
        [weakSelf progressHUDShowWithCompleteText:@"登录失败，请重新登录" isSucceed:NO];
    }];
}


@end
